package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/admin/common")
@Api("阿里云接口")
public class OssController {
    @Autowired
    AliOssUtil aliOssUtil;
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        log.info("文件上传：{}",file);
        try {
            //获取原文件
            String originalFilename = file.getOriginalFilename();
            //截取原文件名后缀
            String substringname = originalFilename.substring(originalFilename.lastIndexOf("."));
            //拼接文件名
            String newflie=UUID.randomUUID().toString()+substringname;
            //获取文件路径
            String filepath = aliOssUtil.upload(file.getBytes(), newflie);
            return Result.success(filepath);
        }catch (Exception e){
            log.error("文件上传失败：",e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);//文件上传失败


    }



}
